//
//  SAMyAccountVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/5/11.
//

#import "SAMyAccountVC.h"
#import "SAMyAccountInfoCell.h"
#import "SAMyAccountModifyPasswordCell.h"
#import "GFAlertView.h"
#import "SALoginVC.h"
#import "SAModifyPasswordVC.h"
#import "SALogoutAPI.h"

@interface SAMyAccountVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIButton *exitBtn;
@property (nonatomic, strong) NSArray *sectionArr;
@property (nonatomic, strong) NSArray *infoTitleArr;
@property (nonatomic, strong) NSMutableArray *infoContentArr;
@property (nonatomic, strong) UIButton *logoutBtn; //注销
@property (nonatomic, strong) SALogoutAPI *logoutAPI;

@end

@implementation SAMyAccountVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [SAValueManger shareInStance].userInfo = nil;
    [kUserDefault setObject:@"" forKey:kAotuLoginKey];
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[SALoginVC new]];
    window.rootViewController = nav;
    [window makeKeyAndVisible];
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    NSDictionary *dic = error.userInfo;
    NSString *msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
    [Prompt popPromptViewWithMsg:msg duration:2];
}


#pragma mark - event
- (void)clickExitBtn {
    GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"您确定要退出登录吗?" knowBtnBlock:^(UIButton *knowBtn) {
        [knowBtn addTarget:self action:@selector(ExitLogin) forControlEvents:UIControlEventTouchUpInside];
    }];
    [alertView showWithAnimation];
}

-(void)ExitLogin {
    [SAValueManger shareInStance].userInfo = nil;
    [kUserDefault setObject:@"" forKey:kAotuLoginKey];
    UIWindow *window =  [UIApplication sharedApplication].keyWindow;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[SALoginVC new]];
    window.rootViewController = nav;
    [window makeKeyAndVisible];
}

- (void)clickLogoutBtn {
    GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"账号注销后不能再使用该APP功能，您确定要注销吗?" knowBtnBlock:^(UIButton *knowBtn) {
        [knowBtn addTarget:self action:@selector(ExitLogout) forControlEvents:UIControlEventTouchUpInside];
    }];
    [alertView showWithAnimation];
}

-(void)ExitLogout {
    [self.logoutAPI sendRequestToServerWithActivityViewAndMask:POST];
}

- (void)initData {
    //员工类型 1-老板 2-店长 3-营业员 4-收银员
    NSString *position = @"老板";
    SAUserInfo *userInfo = [SAValueManger shareInStance].userInfo;
    if (userInfo.userType == 2) {
        position = @"店长";
    }else if (userInfo.userType == 3) {
        position = @"营业员";
    }else if (userInfo.userType == 4) {
        position = @"收银员";
    }
    NSString *name = userInfo.empName;
//    NSString *experience = kExperience;//登录的时候就记录是否是体验账号
//    if (experience.length > 0 && [experience isEqualToString:@"1"]) {
//        if ([SAValueManger shareInStance].userInfo.userType == 3) {
//            name = @"体验员工";
//            self.title = @"体验员工";
//        }else {
//            name = @"体验老板";
//            self.title = @"体验老板";
//        }
//    }
    [self.infoContentArr addObjectsFromArray:@[userInfo.mobile, userInfo.storeName, name, position]];
}

#pragma mark - initUI

- (void)initUI {
    
    [self setNavigationItemBackBBIAndTitle:[SAValueManger shareInStance].userInfo.empName];
    [self setBgImage:nil];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(kScaleNum(StatusHeight), 0, 0, 0));
    }];
}

#pragma mark - UITableView Delegate & DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.infoTitleArr.count;
    } else {
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SAMyAccountInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAMyAccountInfoCell"];
        NSString *title = self.infoTitleArr[indexPath.row];
        NSString *info = self.infoContentArr[indexPath.row];
        [cell refreshCellWithTitle:title info:info];
        return cell;
        
    } else {
        SAMyAccountModifyPasswordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAMyAccountModifyPasswordCell"];
        if (indexPath.row == 0) {
            cell.titleLab.text = @"注销帐号";
            cell.contentLab.hidden = NO;
        }else {
            cell.titleLab.text = @"修改密码";
            cell.contentLab.hidden = YES;
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    UILabel *textLab = [[UILabel alloc] init];
    textLab.textColor = kBlackColor;
    textLab.font = [UIFont systemFontOfSize:16.0];
    textLab.text = self.sectionArr[section];
    [view addSubview:textLab];
    [textLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.centerY.mas_equalTo(0);
    }];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [self clickLogoutBtn];
        }else {
            SAModifyPasswordVC *vc = [[SAModifyPasswordVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

#pragma mark - lazy init
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kLineColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.bounces = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = self.footerView;
        [_tableView registerClass:[SAMyAccountInfoCell class] forCellReuseIdentifier:@"SAMyAccountInfoCell"];
        [_tableView registerClass:[SAMyAccountModifyPasswordCell class] forCellReuseIdentifier:@"SAMyAccountModifyPasswordCell"];
    }
    return _tableView;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 140)];
        [_footerView addSubview:self.exitBtn];
        [self.exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(70).priority(MASLayoutPriorityDefaultMedium);
            make.right.mas_equalTo(-70).priority(MASLayoutPriorityDefaultMedium);
            make.top.mas_equalTo(30);
            make.height.mas_equalTo(40);
        }];
//        SAUserInfo *userInfo = [SAValueManger shareInStance].userInfo;
//        if (userInfo.userType == 1) {
//            [_footerView addSubview:self.logoutBtn];
//            [self.logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.mas_equalTo(70).priority(MASLayoutPriorityDefaultMedium);
//                make.right.mas_equalTo(-70).priority(MASLayoutPriorityDefaultMedium);
//                make.bottom.mas_equalTo(0);
//                make.height.mas_equalTo(40);
//            }];
//        }
    }
    return _footerView;
}

- (UIButton *)exitBtn {
    if (!_exitBtn) {
        _exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_exitBtn setBackgroundImage:[UIImage imageNamed:@"button_blue_bg"] forState:UIControlStateNormal];
        [_exitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_exitBtn setTitle:@"退出" forState:UIControlStateNormal];
        _exitBtn.titleLabel.font = kBFont(16);
        [_exitBtn addTarget:self action:@selector(clickExitBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _exitBtn;
}

- (UIButton *)logoutBtn {
    if (!_logoutBtn) {
        _logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_logoutBtn setTitleColor:kLightgGrayColor forState:UIControlStateNormal];
        [_logoutBtn setTitle:@"注销账号" forState:UIControlStateNormal];
        _logoutBtn.titleLabel.font = kBFont(14);
        [_logoutBtn addTarget:self action:@selector(clickLogoutBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutBtn;
}

- (NSArray *)sectionArr {
    if (!_sectionArr) {
        _sectionArr = [NSArray arrayWithObjects:@"基本信息", @"账号安全", nil];
    }
    return _sectionArr;
}

- (NSArray *)infoTitleArr {
    if (!_infoTitleArr) {
        _infoTitleArr = [NSArray arrayWithObjects: @"手机", @"店铺", @"姓名", @"岗位", nil];
    }
    return _infoTitleArr;
}

- (NSMutableArray *)infoContentArr {
    if (!_infoContentArr) {
        _infoContentArr = [NSMutableArray array];
    }
    return _infoContentArr;
}

-(SALogoutAPI *)logoutAPI {
    if (!_logoutAPI) {
        _logoutAPI = [SALogoutAPI apiWithDelegate:self];
    }
    return _logoutAPI;
}

@end

